package com.mlink.vs.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccUserSettings;
import com.mlink.vs.service.DbTimeService;
import com.mlink.vs.service.PiccUserSettingsService;
import com.mlink.vs.util.StreamUtil;

/**
 * 用户设置接口
 * @author Administrator
 */
public class UserdataAction {
	
	
	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	PiccUserSettingsService piccUserSettingsService;
	
	@Resource
	DbTimeService dbTimeService;

	//保存用户设置
	@Execute(validator = false)
	public String save(){
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		
		Map<String, String> stsMap = new HashMap<String, String>();

		gson = new Gson();
		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String uid = (String) mapData.get("uid");
		String data = (String) mapData.get("data");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(uid)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "uid不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}

			if (StringUtils.isBlank(data)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "data不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			
			PiccUserSettings piccUserSettings = piccUserSettingsService.findById(uid);
			Timestamp dbTimeStamp = dbTimeService.getDbTimeStamp();
			if(null==piccUserSettings){
				PiccUserSettings piccUserSettings2 = new PiccUserSettings();
				piccUserSettings2.userId=uid;
				piccUserSettings2.userSettings=data;
				piccUserSettings2.createTime=dbTimeStamp;
				piccUserSettingsService.insert(piccUserSettings2);
				stsMap.put("sts", "0");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}else{
				piccUserSettings.userSettings=data;
				piccUserSettings.updateTime=dbTimeStamp;
				piccUserSettingsService.update(piccUserSettings);
				stsMap.put("sts", "0");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	//获取用户设置
	@Execute(validator = false)
	public String load(){
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		
		Map<String, String> stsMap = new HashMap<String, String>();

		gson = new Gson();
		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String uid = (String) mapData.get("uid");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(uid)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "uid不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			
			PiccUserSettings piccUserSettings = piccUserSettingsService.findById(uid);
			if(null==piccUserSettings){
				stsMap.put("sts", "1");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}else{
				stsMap.put("sts", "0");
				stsMap.put("data", piccUserSettings.userSettings);
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
}